package com.ittest.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ittest.common.ResultFactory;
import com.ittest.common.exception.BusinessException;
import com.ittest.mapper.DeptMapper;
import com.ittest.mapper.EmployeeMapper;
import com.ittest.pojo.Employee;
import com.ittest.service.DeptService;
import com.ittest.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {

    // 注入dao
    @Autowired
    private EmployeeMapper employeeMapper;



    /**
     * 修改员工信息
     *
     * @param employee 封装员工对象
     * @return
     */
    @Override
    public boolean update(Employee employee) {
        return employeeMapper.updateEmp(employee);
    }

    /**
     * 新增员工信息，包括所属部门
     *
     * @param employee 员工对象
     * @return
     */
    @Override
    public boolean add(Employee employee) {
        return employeeMapper.addEmp(employee);
    }

    /**
     * 员工登录验证
     *
     * @param userName 用户名
     * @param password 密码
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public Employee login(String userName, String password) {
        if (StringUtils.isEmpty(userName)){
            throw new BusinessException(ResultFactory.ERROR("用户名不能为空！"));
        }
        if (StringUtils.isEmpty(password)){
            throw new BusinessException(ResultFactory.ERROR("密码不能为空！"));
        }
        Employee employee = employeeMapper.findByUserNameAndPassword(userName, password);
        if (employee == null){
            throw new BusinessException(ResultFactory.ERROR("用户名或密码错误！"));
        }
        return employee;
    }

    /**
     * 根据部门id查询用户信息
     *
     * @param dept_id   部门id
     * @param page 当前页
     * @param size 每页条数
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public Page<Employee> getEmps(Integer dept_id, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        Page<Employee> list = (Page<Employee>) employeeMapper.findByDeptId(dept_id);
        return list;
    }

    /**
     * 删除单个员工
     *
     * @param id 员工id
     * @return
     */
    @Override
    public boolean delete(Integer id) {
        return employeeMapper.deleteEmp(id);
    }

    /**
     * 查询所有
     * @param page
     * @param size
     * @return
     */

}
